import wxUtil from './wx/WxUtil'

/**
 * 开启授权
 */
exports.startWxlogin = function(thatApp) {
  const app = thatApp;

  wxUtil.login().then(res => {
    //没有微信登录，发送code给后台再次登录
    doLoginProcess(app, res.code);
  });

}

/**
 * 向后台发送code后验证是否有授权
 */
function checkEmpower(thatApp) {
  const app = thatApp;
  wxUtil.getSetting().then(res => {
    //已有用户信息权限
    console.log(JSON.stringify(res));
    if (res.authSetting['scope.userInfo']) {
      // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
      app.loginProgressManager.needAuth = false;
      return wxUtil.getUserInfo();
      // return Promise.reject();
    } else {
      app.loginProgressManager.needAuth = true;
      //如果首页初始化比网络请求慢，首页用这个判断流程是否走完
      app.loginProgressManager.loginProcessComplete = true;
      //有些页面不需要绑定和登录，所以不判断用户有没有绑定过登陆过然后直接跳到登录页，交给当前页处理逻辑
      if (app.loginProgressManager.loginProcessCompleteCallback) {
        //说明首页的初始化比网络请求快
        app.loginProgressManager.loginProcessCompleteCallback();
      }
      wx.reLaunch({
        url: '/pages/login-process/splash/splash',
        success: (result) => {},
        fail: () => {},
        complete: () => {}
      });
      return Promise.reject();
    }
  }).then(res => {
    console.log("授权拿到的信息 " + JSON.stringify(res))
    if (!res.rawData || !res.signature || !res.encryptedData || !res.iv) {
      wx.showToast({
        title: '缺少用户授权信息',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result) => {

        },
        fail: () => {},
        complete: () => {}
      });
      app.loginProgressManager.needAuth = true;
      //如果首页初始化比网络请求慢，首页用这个判断流程是否走完
      app.loginProgressManager.loginProcessComplete = true;
      //有些页面不需要绑定和登录，所以不判断用户有没有绑定过登陆过然后直接跳到登录页，交给当前页处理逻辑
      if (app.loginProgressManager.loginProcessCompleteCallback) {
        //说明首页的初始化比网络请求快
        app.loginProgressManager.loginProcessCompleteCallback();
      }
      wx.reLaunch({
        url: '/pages/login-process/splash/splash',
        success: (result) => {},
        fail: () => {},
        complete: () => {}
      });
      return;
    }
    app.globalData.wechatUserInfo = res;
    const userInfoManager = app.userInfoManager;
    if (userInfoManager.isLogin()) {} else {
      wx.reLaunch({
        url: '/pages/login-process/login/login',
        success: (result) => {

        },
        fail: () => {},
        complete: () => {}
      });
    }
    //如果首页初始化比网络请求慢，首页用这个判断流程是否走完
    app.loginProgressManager.loginProcessComplete = true;
    //有些页面不需要绑定和登录，所以不判断用户有没有绑定过登陆过然后直接跳到登录页，交给当前页处理逻辑
    if (app.loginProgressManager.loginProcessCompleteCallback) {
      //说明首页的初始化比网络请求快
      app.loginProgressManager.loginProcessCompleteCallback();
    }
  })
}

/**
 * 获取完微信的code后
 * 进行正式登录流程
 */
function doLoginProcess(thatApp, code) {
  const app = thatApp;
  const http = app.http;
  const mpAppInfo = app.mpAppInfo;
  checkEmpower(thatApp);
  // return http.sendCode({
  //   code,
  // }).then(res => {
  //   checkEmpower(thatApp);

  // })
}